from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.hashers import make_password
from django.http import JsonResponse
from django.views import View
from django.contrib.auth import get_user_model

User = get_user_model()


class RegisterView(View):
    """
    用户注册
    """

    def get(self, request):
        return render(request, 'register.html')

    def post(self, request, *args, **kwargs):
        # 获取用户直接插入到数据库中
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        new_password = make_password(password=password)
        res = User.objects.create(username=username, password=new_password)
        if res:
            return JsonResponse({'code': 0, 'message': '成功'})

        return JsonResponse({'code': 1, 'message': '失败'})


class LoginView(View):
    """
    用户登陆
    """

    def get(self, request, *args, **kwargs):
        return render(request, 'login.html')

    def post(self, request, *args, **kwargs):
        # 获取用户名及密码
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)

        user = authenticate(username=username, password=password)
        print(user)
        if user:
            # 登陆成功
            login(request, user)
            return JsonResponse({'code': 0, 'message': '登录成功'})
        return JsonResponse({'code': 1, 'message': '登录失败'})


from django.http import HttpResponse
from geetest import GeetestLib

# 请在官网申请ID使用，示例ID不可使用
pc_geetest_id = "8f4818bd2dea5520c29e40a08c3d74a1"
pc_geetest_key = "7ddae73f8684dcba0314f3a2eca34339"


class GeetestView(View):
    """
    获取滑动验证码
    """

    def get(self, request, *args, **kwargs):
        """
        生成极速验证码
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        user_id = 'test'
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        status = gt.pre_process(user_id)
        request.session[gt.GT_STATUS_SESSION_KEY] = status
        request.session["user_id"] = user_id
        response_str = gt.get_response_str()
        return HttpResponse(response_str)

    def post(self, request, *args, **kwargs):
        """
        验证极速验证码
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        challenge = request.POST.get(gt.FN_CHALLENGE, '')
        validate = request.POST.get(gt.FN_VALIDATE, '')
        seccode = request.POST.get(gt.FN_SECCODE, '')
        status = request.session[gt.GT_STATUS_SESSION_KEY]
        user_id = request.session["user_id"]

        if status:
            result = gt.success_validate(challenge, validate, seccode, user_id)
        else:
            result = gt.failback_validate(challenge, validate, seccode)

        if result:
            return JsonResponse({'code': 0, 'message': '验证成功'})
        return JsonResponse({'code': 1, 'message': '登录失败'})


class LogoutView(View):
    """
    退出登录
    """

    def get(self, request, *args, **kwargs):
        logout(request)
        return redirect("user:login")


def error404(request):
    """
    400错误页面
    :param request:
    :return:
    """
    response = render(request, '404.html')
    response.status_code = 404
    return response


def error500(request):
    """
    500错误页面
    :param request:
    :return:
    """
    response = render(request, '500.html')
    response.status_code = 500
    return response
